package cn.itbeien.payment.message;

import org.apache.rocketmq.common.message.MessageConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author beien
 * @date 2024-03-22 14:57
 * Copyright© 2024 beien
 * 用于发送消息的工具类
 */
@Component
public class SendMessage {
    @Autowired
    StreamBridge streamBridge;

    public void sendDelayMsg(String key,String id,String delayLevel,SimpleMsg simpleMsg){
        //消息头
        Map<String, Object> headers = new HashMap<>();
        headers.put(MessageConst.PROPERTY_KEYS, key);
        headers.put(MessageConst.PROPERTY_ORIGIN_MESSAGE_ID,id);
        // 设置延时等级1~10  延迟30秒发送消息
        headers.put(MessageConst.PROPERTY_DELAY_TIME_LEVEL, delayLevel);
        Message<SimpleMsg> msg = new GenericMessage(simpleMsg, headers);
        streamBridge.send("producer-out-0", msg);//从nacos中读取
    }
}
